<?php

namespace App\Admin\Controllers\DataView;

use App\Models\UserAddRmb;
use App\Models\UserLogin;

use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class OverviewController extends Controller
{
    use ModelForm;
    private $servers;

    public function __construct()
    {
        $this->servers = $this->getServers(date('Ym'));
        parent::__construct();
    }

    /**
     * 数据总览--实时数据
     */
    public function realData()
    {
        $config = config('admin.data.real_data');

        return Admin::content(function (Content $content) use ($config) {

            $content->header($config['label']);
            $content->description(date("Ymd", time()));

            $servers = $this->servers;
            $registers = $this->getRegisteredUsersByDate(date("Ymd", time()));

            $headers = ['区服', '实时在线', '前日在线环比', '当日充值总计', '前日充值环比', '当日付费用户数', '本日登陆用户数', '本日注册用户数', '本日创角用户数', '本日实时转化率', '本月充值总计'];
            $rows = [];
            $userAddRmb = new UserAddRmb();
            $userLogin = new UserLogin();
            $totalCreatedRoleUsers = 0;
            $totalLoginedUsers = 0;
            $totalRegisteredUsers = 0;
            foreach ($servers as $serv) {
                $r0 = $serv;
                $r1 = $userLogin->getCurrentOnlineUsers(time(), $serv);
                $r2 = '';
                $r3 = $userAddRmb->getDayRechargeAmounts(time(), $serv);
                $r4 = 0;
                $r5 = $userAddRmb->getDayRechargeUsers(time(), $serv);
                $r6 = $userLogin->getDayLoginedUsers(time(), $serv);
                $r7 = $registers;
                $r8 = $userLogin->getDayCreatedRoleUsers(time(), $serv);;
                $r9 = $r7 ? round($r8 / $r7, 4) * 100 . '%' : 0;
                $r10 = $userAddRmb->getMonthRechargeAmounts(time(), $serv);
                $rows[] = [
                    $r0, //区服
                    $r1, //实时在线
                    $r2, //前日在线环比
                    $r3, //当日充值总计
                    $r4, //前日充值环比
                    $r5, //当日付费用户数
                    $r6, //本日登陆用户数
                    $r7, //本日注册用户数
                    $r8, //本日创角用户数
                    $r9, //本日实时转化率
                    $r10, //本月充值总计
                ];
                $totalCreatedRoleUsers += $r8;
                $totalLoginedUsers += $r6;
                $totalRegisteredUsers = $r7;
            }
            $rate = $totalRegisteredUsers ? round($totalCreatedRoleUsers / $totalRegisteredUsers, 4) * 100 . '%' : '-';
            $rows[] = ['总计', '', '', '', '', '', $totalLoginedUsers, $totalRegisteredUsers, $totalCreatedRoleUsers, $rate, ''];

            $table = new Table($headers, $rows);

            $refreshBtn = $this->refreshBtn;
            $tab = new Tab();
            $tab->add('列表', $refreshBtn . $table);
            $content->row($tab);
        });
    }

    /**
     * 数据总览--历史数据
     * @param Request $request
     * @return mixed
     */
    public function historyData(Request $request)
    {
//        $date = $request->get('date') ?: date('Y-m-d');
        $config = config('admin.data.history_data');
        $date_start = $request->get('date_start') ?: date('Y-m-d');
        $date_end = $request->get('date_end') ?: date('Y-m-d');
        $server = $request->get('server', 2);

        return Admin::content(function (Content $content) use ($config, $date_start, $date_end, $server) {

            $servers = $this->servers;

            $content->header($config['label']);
            $content->description($date_start.' ~ '.$date_end);

            $headers = ['日期', '充值总计', '付费用户数', '付费次数', 'arpu', '付费率', '注册用户数', '创角用户数', '用户转化率', '注收比', '日活跃用户'];
            $rows = [];
            $dateRange = $this->getDateFromRange($date_start, $date_end);
            foreach ($dateRange as $d) {
                $date = strtotime($d);
                $userLogin = new UserLogin(date('Ym', $date));
                $userAddRmb = new UserAddRmb(date('Ym', $date));
                $totalRegisteredUsers = 0;
                $totalCreatedRoleUsers = 0;
                $totalLoginedUsers = 0;
                $totalRechargeAmounts = 0;
                $totalRechargeUsers = 0;
                $totalRechargeTimes = 0;
                $registers = $this->getRegisteredUsersByDate($d);
                foreach ($servers as $serv) {
                    $r0 = $serv;
                    $r1 = $userAddRmb->getDayRechargeAmounts($date, $serv);
                    $r2 = $userAddRmb->getDayRechargeUsers($date, $serv);
                    $r3 = $userAddRmb->getDayRechargeTimes($date, $serv);
                    $r4 = $r3 ? round($r1 / $r3, 4) : $r3;
                    $r7 = $userLogin->getDayCreatedRoleUsers($date, $serv);
                    $r5 = $r7 ? round($r2 / $r7, 4) * 100 . '%' : $r7;
                    $r6 = $registers;
                    $r8 = $r6 ? round($r7 / $r6, 4) * 100 . '%' : $r6;
                    $r9 = $r6 ? round($r1 / $r6, 4) : $r6;
                    $r10 = $userLogin->getDayLoginedUsers($date, $serv);
//                $rows[] = [
//                    $r0,
//                    $r1, //充值总计
//                    $r2, //付费用户数
//                    $r3, //付费次数
//                    $r4, //apru
//                    $r5, //付费率
//                    $r6, //注册用户数
//                    $r7, //创角用户数
//                    $r8, //用户转化率
//                    $r9, //注收比
//                    $r10, // 日活跃用户
//                ];
                    $totalRechargeAmounts += $r1;
                    $totalRechargeUsers += $r2;
                    $totalRechargeTimes += $r3;
                    $totalCreatedRoleUsers += $r7;
                    $totalLoginedUsers += $r10;
                    $totalRegisteredUsers = $r6;
                }
                $rows[] = [$d, $totalRechargeAmounts, $totalRechargeUsers, $totalRechargeTimes, $totalRechargeTimes ? round($totalRechargeAmounts / $totalRechargeTimes, 4) : $totalRechargeTimes, $totalCreatedRoleUsers ? round($totalRechargeUsers / $totalCreatedRoleUsers, 4) * 100 . '%' : $totalCreatedRoleUsers, $totalRegisteredUsers, $totalCreatedRoleUsers, $totalRegisteredUsers ? round($totalCreatedRoleUsers / $totalRegisteredUsers, 4) * 100 . '%' : '-', '', $totalLoginedUsers];
            }
            $table = new Table($headers, $rows);
            $table->id('paginate')->paginate();
            $tab = new Tab();
            $tab->add('列表', $table);

            $content->row(function (Row $row) use ($tab, $date) {

                $row->column(7, function (Column $column) use ($tab) {
                    $column->append($tab);
                });

                // 查询条件表单
                $row->column(5, function (Column $column) use ($date) {
                    $form = new Form();
                    $form->dateRange('date_start', 'date_end', '日期范围');
                    $form->method('post');
                    $form->action(admin_url('dataView/overviewHistoryData'));
                    // $form->disablePjax();
                    $column->append((new Box('按日期查询', $form))->style('primary')->solid());
                });

            });
        });
    }

    /**
     * 获得区服信息
     * @param $date
     * @return mixed
     */
    public function getServers($date)
    {
        $servers = [];
        $userLogin = new UserLogin($date);
        $res = $userLogin->select('servername')->groupBy('servername')->get()->toArray();
        foreach ($res as $r) {
            $servers[] = $r['servername'];
        }

        return $servers;
    }

    /**
     *
     * 根据日期从97time_uc获得日注册人数
     * @param $date
     * @return bool|string
     */
    public function getRegisteredUsersByDate($date)
    {
        $api_uri = "http://www.97time.com/adminlog.php/Index/getRegisteredUsersByDate?date=" . $date;
        $res = file_get_contents($api_uri);

        return $res;
    }

}
